//
//  OilPriceViewController.swift
//  TrafficViolation
//
//  Created by clover on 2017/5/3.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class OilPriceViewController: BaseController {
    var oilTable:UITableView!
    var oilArray: [OilModel]!
    // 头部视图
    lazy var headerView: OilHeaderView = {
        let view = OilHeaderView(frame: CGRect(x: 0, y: kStatusBarH + kNavigationBarH, width: kScreenW, height: 40))
        return view
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setupNavBar(title: "今日油价", isback: true, appearWay: 2, isright: false)//自定义导航
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        oilArray = Array<OilModel>()
        loadOilList()
        //初始化表格
        oilTable = UITableView(frame: CGRect(x:0,y:kStatusBarH + kNavigationBarH + 40,width:kScreenW,height:kScreenH - kStatusBarH - kNavigationBarH - 40),style: UITableViewStyle.plain)
        oilTable.delegate = self
        oilTable.dataSource = self
        oilTable.backgroundColor = tableBgColor
        oilTable.rowHeight = 35
        self.view.addSubview(oilTable)
        self.view.addSubview(headerView)
    }
}
extension OilPriceViewController{
    //查询今日油价
    func loadOilList(){
        let urlString = "\(projectUrl)/car/oil/list/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"
        Alamofire.request(urlString, method: .get, parameters: nil, headers : headers).responseJSON { (response) in
            if response.result.isSuccess{
                let json = JSON(response.result.value as! [String : AnyObject])
                if json["success"] == true {
                    for (_,subJson) : (String,JSON) in json["rdata"] {
                        let model = OilModel.init(dict: subJson)
                        self.oilArray.append(model)
                    }
                    if self.oilArray.count > 0 {
                        self.oilTable.reloadData()
                    }
                }
                else{
                    NSLog("获取今日油价信息出错啦!\(json["rdata"])")
                    self.failOperNoticeFun(code: json["rcode"].rawString()!, message: json["rdata"].rawString()!)
                }
            }
            else{
                print(response.result.error!) //输出错误日志
            }
        }
    }
}
extension OilPriceViewController:UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return oilArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell: OilViewCell? = tableView.dequeueReusableCell(withIdentifier: "oilCellID") as? OilViewCell
        if cell == nil {
            cell = OilViewCell(style: UITableViewCellStyle.default,reuseIdentifier: "oilCellID")
        }
        if oilArray.count > 0 {
            let model = oilArray[(indexPath as NSIndexPath).row]
            cell?.configCellDateWithModel(model: model)
        }
        return cell!
    }
    //选中cell时触发这个代理
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        tableView.deselectRow(at: indexPath, animated: false)//点击完成，取消高亮
    }
}

